<script setup>
import { onMounted, ref } from 'vue'
import { getUserFollowList } from '@/api/userFollow.js'
import { useRouter } from 'vue-router'
const router = useRouter()
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)
const loading = ref(true)

const tableData = ref([])
const getTableData = async () => {
  loading.value = true
  const res = await getUserFollowList({
    pageNum: pageNum.value,
    pageSize: pageSize.value
  })
  tableData.value = res.data.records
  total.value = res.data.total
  loading.value = false
}
onMounted(getTableData)

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}
const handleCurrentChange = (val) => {
  pageNum.value = val
  getTableData()
}

const goToSongList = (userId) => {
  router.push({
    path: '/home/song/list',
    query: {
      userId
    }
  })
}
</script>

<template>
  <div class="user-follow">
    <h2 class="page-title">My Follow</h2>
    <div v-if="loading" class="loading">Loading...</div>
    <div v-else>
      <div class="user-list">
        <div
          v-for="user in tableData"
          :key="user.id"
          class="user-item"
          @click="goToSongList(user.id)"
        >
          <div class="user-avatar">
            <img
              :src="user.avatar || '/path/to/default/avatar.png'"
              alt="avatar"
              class="avatar"
            />
          </div>
          <div class="user-details">
            <div class="detail-item">
              <strong>Username:</strong> {{ user.username }}
            </div>
            <div class="detail-item">
              <strong>Email:</strong> {{ user.email || 'Not provided' }}
            </div>
            <div class="detail-item">
              <strong>Created:</strong> {{ user.createTime }}
            </div>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          :current-page="pageNum"
          :page-size="pageSize"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, prev, pager, next, sizes"
          :page-sizes="[10, 20, 50, 100]"
        />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.user-follow {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;

  .page-title {
    text-align: center;
    font-size: 2rem;
    color: #333;
    margin-bottom: 20px;
  }

  .loading {
    text-align: center;
    font-size: 1.2rem;
    color: #888;
  }

  .user-list {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    margin-bottom: 20px;
  }

  .user-item {
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    padding: 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    max-width: 1000px;
    transition:
      transform 0.3s ease,
      box-shadow 0.3s ease;
    margin: 0 auto;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2);
      cursor: pointer;
    }

    @media (max-width: 768px) {
      width: 100%;
      flex-direction: column;
      align-items: flex-start;
    }
  }

  .user-avatar {
    flex-shrink: 0;
    margin-right: 20px;
  }

  .avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    object-fit: cover;
  }

  .user-details {
    display: flex;
    flex-direction: row;
    gap: 8px;
    width: 100%;
  }

  .detail-item {
    font-size: 1rem;
    color: #555;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    width: 100%;
  }

  .pagination-container {
    text-align: center;
    margin-top: 20px;
  }
}
</style>
